﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CamShared
{

    public abstract class BaseCamera : ICamera
    {
        private Image LoadingImage
        {
            get
            {
                Bitmap b = new Bitmap(640, 480);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.DrawString("Loading...", new Font("Verdana", 24), Brushes.Lime, new PointF(20, 20));
                }
                return b;
            }
        }


        public abstract bool ShowConfig(IntPtr handle);


        public event CamShared.FrameReceived OnFrame;
        public event CamShared.CameraError OnError;

        public List<string> Attributes { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }

        private static int UIDCount = 0;
        private int myUID = UIDCount++;

        public string UID
        {
            get
            {
                return Class + "_" + Name + "_" + myUID;
            }
        }

        public int FrameCount
        {
            get;set;
        }

        public int Sessions
        {
            get;set;
        }

        public void Activate()
        {
            Sessions++;
            if (Sessions == 1)
            {
                FrameArrived(this, LoadingImage);
                OnActivated();
            }
        }

        protected abstract void OnActivated();
        protected abstract void OnDeActivated();
       

        protected void FrameArrived(ICamera cam, System.Drawing.Image img)
        {
            FrameCount++;
            if (OnFrame != null)
            {
                OnFrame.Invoke(cam, img);
            }
        }

        protected void ErrorArrived(ICamera cam, string error, bool fatal)
        {
            if (OnError != null)
            {
                OnError.Invoke(cam, error, fatal);
            }
        }


        public void Deactivate()
        {
            Sessions--;
            if( Sessions == 0)
            {
                OnDeActivated();
            }
        }

        public bool Active
        {
            get { return Sessions > 0; }
        }

        public void Dispose()
        {
            
        }

    }
   
}
